package com.example.farmeri.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmeri.R;
import com.example.farmeri.pojo.Message;
import com.example.farmeri.pojo.Messages;
import com.example.farmeri.tools.DataUtils;
import com.example.farmeri.ui.news.NewscheckActivity;

import java.util.ArrayList;

/**
 * Create by weiqishan on 2020/4/10.
 */
public class NoticeAdapter extends RecyclerView.Adapter {

    Context context;
    Integer layout;
    ArrayList<Message> messageArray;
    Integer messageCount;

    // 普通布局
    private final int TYPE_NORMAL = 1;
    // 脚布局
    private final int TYPE_FOOTER = 2;
    // 当前加载状态，默认为加载完成
    private int loadState = 1;
    // 正在加载
    public final int LOADING = 1;
    // 加载完成
    public final int LOADING_COMPLETE = 2;
    // 加载到底
    public final int LOADING_END = 3;

    public NoticeAdapter(Context context, int layout, Messages data) {
        this.context = context;
        this.layout = layout;
        this.messageArray = data.getMessageListTop();
        this.messageCount = data.getMessageListCount();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        if(i == TYPE_NORMAL){
            View itemView = inflater.inflate(layout,viewGroup,false);
            return new NoticeViewHolder(itemView);
        }else if (i == TYPE_FOOTER){
            View itemView = inflater.inflate(R.layout.footer,viewGroup,false);
            return new NoticeFootViewHolder(itemView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof NoticeViewHolder) {
            NoticeViewHolder mViewHolder = (NoticeViewHolder) viewHolder;
            Message message = messageArray.get(i);
            mViewHolder.title.setText(message.getTitle());
            mViewHolder.content.setText(message.getContent());
            String date = DataUtils.getStringDate(message.getUpdateDate());
            mViewHolder.date.setText(date);

            mViewHolder.container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, NewscheckActivity.class);
                    Message message = messageArray.get(mViewHolder.getAdapterPosition());
                    String date = DataUtils.getStringDate(message.getUpdateDate());
                    intent.putExtra("title", message.getTitle());
                    intent.putExtra("content", message.getContent());
                    intent.putExtra("messageType", message.getMessageType());
                    intent.putExtra("remarks", message.getRemarks());
                    intent.putExtra("date", date);
                    context.startActivity(intent);
                }
            });
        }else if (viewHolder instanceof NoticeFootViewHolder){
            NoticeFootViewHolder mViewHolder = (NoticeFootViewHolder)viewHolder;
            switch(loadState){
                case LOADING:
                    mViewHolder.container_loading.setVisibility(View.VISIBLE);
                    mViewHolder.container_complete.setVisibility(View.GONE);
                    break;
                case LOADING_COMPLETE: // 加载完成
                    mViewHolder.container_loading.setVisibility(View.GONE);
                    mViewHolder.container_complete.setVisibility(View.GONE);
                    break;

                case LOADING_END: // 加载到底
                    mViewHolder.container_loading.setVisibility(View.GONE);
                    mViewHolder.container_complete.setVisibility(View.VISIBLE);
                default:
                    break;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        //if (position < data.size()){
        //    return TYPE_NORMAL;
        //}
        if (position < messageArray.size()){
            return TYPE_NORMAL;
        }
        return TYPE_FOOTER;
    }

    public int getLoadState() {
        return loadState;
    }

    /**
     * 设置上拉加载状态
     * @param loadState 0.正在加载 1.加载完成 2.加载到底
     */
    public void setLoadState(int loadState) {
        this.loadState = loadState;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return messageArray.size();
    }

    /**
     * 页脚  底部加载框
     */
    class NoticeFootViewHolder extends RecyclerView.ViewHolder {
        ViewGroup container_loading;
        ViewGroup container_complete;

        NoticeFootViewHolder(View itemView) {
            super(itemView);
            container_loading = itemView.findViewById(R.id.footer_loading);
            container_complete = itemView.findViewById(R.id.footer_complete);
            container_loading.setVisibility(View.GONE);
            container_complete.setVisibility(View.GONE);
        }
    }


    class NoticeViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView title;
        TextView content;
        TextView date;
        ConstraintLayout container;

        public NoticeViewHolder(@NonNull View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.item_notice_container);
            imageView = itemView.findViewById(R.id.item_notice_image);
            title = itemView.findViewById(R.id.item_notice_title);
            content = itemView.findViewById(R.id.item_notice_content);
            date = itemView.findViewById(R.id.item_notice_date);
        }
    }
}
